package cn.qf.shop.controller;

import cn.qf.shop.pojo.entity.Pets;
import cn.qf.shop.pojo.qo.PetsQo;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.PetsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
@EnableSwagger2
@RequestMapping("/pets")
@Api(tags = "宠物管理业务接口")
@RestController
@RestControllerAdvice
public class PetsController extends BaseController{
    private Logger logger= LoggerFactory.getLogger(PetsController.class);
    @Resource
    private PetsService petsService;
    @ResponseBody
    @ApiOperation("宠物信息展示")
    @PostMapping(value = "/selectAllByPets",produces = {"application/json;charset=UTF-8"})
    public RespResult selectAll(){
        List<Pets> pets = petsService.selectAll();
        RespResult respResult=null;
        if (pets==null){
            respResult = super.returnRespWithFailed("找不到用户列表");
        }
        respResult= super.returnRespWithSuccess(pets);
        return respResult;
    }
    @ResponseBody
    @ApiOperation("根据关键字查询")
    @PostMapping(value = "/selectByKeyWordForPets",produces = {"application/json;charset=UTF-8"})
    public RespResult selectByKeyWord(PetsQo petsQo){
        List<Pets> pets = petsService.selectByKeyWord(petsQo);
        RespResult respResult=null;
        if (pets==null){
            respResult = super.returnRespWithFailed("找不到用户列表");
        }
        respResult= super.returnRespWithSuccess(pets);
        return respResult;
    }
    @ResponseBody
    @ApiOperation("批量删除")
    @RequestMapping(value = "/updateByPetsIds",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult updateByPetsIds(@RequestParam(value = "petsIds[]") Set<Integer> petsIds){
        int i = petsService.updateByPetsIds(petsIds);
        RespResult respResult=null;
        if (i==-1){
            respResult = super.returnRespWithFailed("删除失败");
        }
        respResult=selectAll();

        return respResult;
    }
    @ResponseBody
    @ApiOperation("增加数据")
    @RequestMapping(value = "/AddPets",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult AddPets(PetsQo petsQo){
        int i = petsService.AddPets(petsQo);
        RespResult respResult=null;
        if (i==-1){
            respResult = super.returnRespWithFailed("添加失败");
        }
        respResult=selectAll();
        return respResult;
    }
    @ResponseBody
    @ApiOperation("修改数据")
    @RequestMapping(value = "/updateByOnclickForPets",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult updateByOnclickForPets(PetsQo petsQo){
        int i = petsService.updateByOnclick(petsQo);
        RespResult respResult=null;
        if (i==-1){
            respResult.setMessage("修改失败");
        }
        respResult=selectAll();

        return respResult;
    }
    /**
     * 根据点击图标删除所在的行
     * @param petsQo
     * @return
     */
    @ResponseBody
    @ApiOperation("删除一行")
    @RequestMapping(value = "/deleteByPetsId",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult updateByPetsId(PetsQo petsQo){
        int i = petsService.updateByPetsId(petsQo);
        RespResult respResult=null;
        if (i==-1){
            respResult.setMessage("删除失败");
        }
        respResult=selectAll();
        return respResult;
    }
}
